<?php
class Pageley_Commerce_Command_UpdateOrderQuantities extends Pageley_Controller_Command_TransactionalRead {

	private $quantities;

	public function setQuantities($quantities) {
		$this->quantities = $quantities;
	}

	protected function executeInternal() {
		$order = $this->getSession()->get(Pageley_Commerce_Session::ORDER);
		if($order === null) {
            throw new Pageley_Controller_RuntimeException("Attempt to update order quantities when no order is present.");
        }
        foreach($this->quantities as $index => $quantity) {
            $orderItem = $order->get("orderItems")->get($index);
            if($orderItem === null) {
                throw new Pageley_Controller_RuntimeException("No order item found with index " . $index . " in current order.");
            }
            $orderItem->set("quantity", $quantity);
        }
	}
}